import {Request, Response} from "express";
import {checkParams, clone, getDecryptToken} from "@src/util/util";
import CONSTANT from "../../util/constant";
import {
  addUser,
  getJt,
  updateUser,
  userInfo,
  userList,
} from "@src/services/authServer";
import {
  categoryListSql,
  delUserSql,
  findJurisdictionAll,
} from "@src/sql/userSql";

const paths = {
  login: "/login",
  info: "/info",
  add: "/user",
  update: "/user/:id",
  delete: "/user/:id",
  userList: "/user-list",
  categoryList: "/field-category-list",
  jurisdiction: "/jurisdiction",
};

// 登录用戶
const login = async (req: Request, res: Response) => {
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  const flag = checkParams(req.body, ["username", "password"]);
  if (!flag) {
    resObj = CONSTANT.LACK;
    res.json({data: resObj});
  } else {
    // 获取请求参数
    const {username, password} = req.body;
    // 获取响应体
    const data = await getJt(username, password);
    res.json({
      data,
    });
  }
};

// 新增用户
const add = async (req: Request, res: Response) => {
  let resObj = clone(CONSTANT.ADD_DEFAULT_SUCCESS);
  const flag = checkParams(req.body, ["username", "password", "jurisdiction"]);
  if (!flag) {
    resObj = CONSTANT.LACK;
    res.json({data: resObj});
  } else {
    // 获取请求体
    const {username, password, jurisdiction} = req.body;
    // 插入user
    const isFlag = await addUser(username, password, jurisdiction);
    if (!isFlag) {
      resObj = CONSTANT.ADD_ERROR;
    }
    res.json({
      data: resObj,
    });
  }
};

// 用户信息
const info = async (req: Request, res: Response) => {
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  // 获取请求头token
  const token = req.headers.token as string;
  // token解密后
  const tokenInfo = getDecryptToken(token);
  if (tokenInfo.token) {
    // 获取sql
    const data = await userInfo(tokenInfo.data?.id);
    res.json({
      data,
    });
  } else {
    resObj = CONSTANT.TOKEN_ERROR;
    res.json(
      {
        data: resObj,
      },
    );
  }
};

// 更新用户
const update = async (req: Request, res: Response) => {
  const flag = checkParams(req.body, ["username", "jurisdiction"]);
  if (!flag) {
    return res.json({data: CONSTANT.LACK});
  } else {
    const {id} = req.params;
    if (id === 'undefined') {
      return res.json({data: CONSTANT.LACK});
    }
    const {username, jurisdiction} = req.body;
    return res.json({
      data: await updateUser(id, username, jurisdiction),
    });
  }
};

// 删除用户
const _delete = async (req: Request, res: Response) => {
  const {id} = req.params;
  if (!id) {
    return res.json({data: CONSTANT.LACK});
  }
  await delUserSql(id);
  return res.json({data: clone(CONSTANT.DEFAULT_SUCCESS)});
};

// 用户列表
const list = async (req: Request, res: Response) => {
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters;
  const data = await userList(Number(page), Number(size), filters ? filters : {});
  res.json({data});
};


// 权限列表
const jurisdictionList = async (req: Request, res: Response) => {
  const data = await findJurisdictionAll();
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  res.json({
    data: {
      ...resObj,
      data,
    },
  });
};

// 字段分类
const categoryList = async (req: Request, res: Response) => {
  let resObj = clone(CONSTANT.DEFAULT_SUCCESS);
  const data = await categoryListSql();
  return res.json({
    data: {
      ...resObj,
      data,
    },
  });
};

export default {
  paths,
  login,
  add,
  info,
  jurisdictionList,
  update,
  _delete,
  list,
  categoryList,
};